<?php

/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/6/3
 * Time: 11:29
 */

namespace app\vehicle\controller;

use think\Controller;
use service\UtilService;
use service\JsonService;
use think\facade\Request;
use think\facade\Log;
use think\Db;

class Index extends Controller
{
    private $data_ip = 'zhwzds.dns0755.net';
    private $data_user = 'sa';
    private $data_num = '1433';
    private $data_pwd = 'Powerpos3721';
    private $data_name = 'PowerDishC';
    private $model = '';

    public function __construct()
    {
        $data_ip = 'localhost';
        $data_user = 'posapp2003';
        $data_num = '3306';
        $data_pwd = 'posapp2003';
        $data_name = 'posapp2003';
        $this->model = Db::connect('mysql://' . $data_user . ':' . $data_pwd . '@' . $data_ip . ':' . $data_num . '/' . $data_name . '#utf8');
        if ($_REQUEST['id']) {
            $query = "select * from poscust where CustID=" . $_REQUEST['id'];
            $hotel =  $this->model->query($query);
            $this->data_ip = $hotel[0]['data_ip'];
            $this->data_user = $hotel[0]['data_user'];
            $this->data_num = $hotel[0]['data_num'] ?? 1433;
            $this->data_pwd = $hotel[0]['data_pwd'];
            $this->data_name = $hotel[0]['data_name'];

            $time = time();
            $s_time = strtotime(date('Y-m-d') . ' ' . $hotel[0]['ShopOpenTime']);
            $e_time = strtotime(date('Y-m-d') . ' ' . $hotel[0]['ShopEndTime']);
            if ($s_time > $e_time) {
                if ($s_time < $time && $e_time + 86400 > $time) {
                } else if ($s_time - 86400 < $time && $e_time > $time) {
                } else {
                    exit(json_encode(array('code' => 100, 'msg' => '该店暂时无营业')));
                }
            } else {
                if ($s_time < $time && $e_time > $time) {
                } else {
                    exit(json_encode(array('code' => 100, 'msg' => '该店暂时无营业')));
                }
            }
        }
    }

    public function login()
    {

        $mobile = Request::param('mobile') ? Request::param('mobile') : '';
        $query = "select * from user where mobile=" . $mobile;
        $list = $this->model->query($query);
        if ($list[0]['UserID']) {
            exit(json_encode(array('code' => 200, 'data' => $list)));
        } else {
            //  $mobile = '13750034090';
            //  $query ="select * from user where mobile=".$mobile;
            //  $list = $this->model->query($query);
            //  exit(json_encode(array('code'=>200,'data'=>$list)));
            exit(json_encode(array('code' => 100, 'msg' => '该手机还没授权，请联系客服。')));
        }
    }

    public function check()
    {
        exit(json_encode(array('code' => 200, 'data' => '')));
    }

    public function api()
    {
        $mobile = Request::param('mobile') ? Request::param('mobile') : '';
        $query = "select * from user where mobile=" . $mobile;
        $list = $this->model->query($query);
        if ($list[0]['UserID']) {
            $query = "select * from poscust where CustID in(" . $list[0]['HotelID'] . ") ORDER BY SqlType desc";
            $row = $this->model->query($query);
            $result = [];
            foreach ((array)$row as $list) {
                $result[] = $list;
            }
            exit(json_encode(array('code' => 200, 'data' => $result)));
        } else {
            exit(json_encode(array('code' => 100, 'msg' => '暂无开通的门店')));
        }
    }

    public function bug()
    {
        $data = input();
        Log::error('手机授权失败：' . json_encode($data));
    }

    public function data()
    {
        $query = "select BussDate,isnull(sum(PersonNum),0) as PersonNum,isnull(sum(PayTotal),0) as Total from eAccount_his where convert(varchar(7),BussDate,120)='" . date('Y-m') . "' group by BussDate";
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        $row = $model->query($query);
        $result = [];
        $day = [];
        $price = [];
        $person = [];
        $max = 0;
        foreach ((array)$row as $list) {
            if ($list['PersonNum'] > $max) {
                $max = $list['PersonNum'];
            }
            $list['BussDate'] = iconv('gb2312', 'utf-8', $list['BussDate']);
            $BussDate = explode(' ', $list['BussDate']);
            $list['day'] = intval(date('m')) . '月' . date('d', strtotime($list['BussDate'])) . '日';
            $result[] = $list;
            $day[] = $list['day'];
            $price[] = $list['Total'];
            $person[] = $list['PersonNum'];
        }
        exit(json_encode(array('row' => $row, 'code' => 200, 'person' => $person, 'max' => $max, 'price' => $price, 'day' => $day, 'data' => $result)));
    }

    public function shop()
    {
        $sql = 'exec wxRecReport';
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        $model->execute($sql);

        $query = "select Project,Content from rBussDayReport order by xh";
        $row = $model->query($query);
        $result = [];
        $arr = [];
        $pie = [];
        $pie_data = [];
        $isPie = false;
        $price = 0;
        foreach ((array)$row as $list) {
            if ($list['Project'] == '总营业额') {
                $list['Content'] = str_replace('.00', '', number_format($list['Content'], 2, '.', ''));
                $result[] = $list;
            } else if ($list['Project'] == '目前营收' || $list['Project'] == '应收金额') {
                $list['Content'] = str_replace('.00', '', number_format($list['Content'], 2, '.', ''));
                $result[] = $list;
            } else if ($list['Project'] == '帐单数' || $list['Project'] == '总 单 数') {
                $result[] = $list;
            } else if ($list['Project'] == '客流量' || $list['Project'] == '总 人 数') {
                $result[] = $list;
            } else if ($list['Project'] == '人均消费') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            } else if ($list['Project'] == '台均消费') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            } else if ($list['Project'] == '退菜金额' || $list['Project'] == '退菜') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            } else if ($list['Project'] == '赠送金额' || $list['Project'] == '赠送') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            } else if ($list['Project'] == '折扣总额') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            }
            if ($isPie) {
                $list['Content'] = str_replace('.00', '', number_format($list['Content'], 2, '.', ''));
                $a = array();
                $a['name'] = $list['Project'] . '￥' . $list['Content'];
                $a['value'] = $list['Content'];
                $pie[] = $a;
                $pie_data[] = $list['Project'] . '￥' . $list['Content'];
                $price += $list['Content'];
            }
            if ($list['Project'] == '付款汇总') {
                $isPie = true;
            }
            $arr[] = $list;
        }
        exit(json_encode(array('code' => 200, 'pie' => $pie, 'price' => $price, 'pie_data' => $pie_data, 'data' => $result)));
    }

    public function caipin()
    {
        $query = "select Top 10 DishName,sum(QtyNum) as QtyNum,sum(Total) as Total from eOrderDish_day group by DishName order by QtyNum Desc";
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        $list = $model->query($query);
        $result = [];
        // $list['QtyNum'] = intval($list['QtyNum']);
        $result = $list;
        foreach ((array)$result as $k => $v) {
            $result[$k]['number'] = $v['QtyNum'] > 0 ? number_format($v['QtyNum'] / $result[0]['QtyNum'] * 100, 2) : 0;
        }
        exit(json_encode(array('code' => 200, 'data' => $result)));
    }

    public function canzhuo()
    {
        $query = "select a.TableName, b.PersonNum ,(Total+HallService+AddService-DiscountTotal) as Total,HallName,IFClose from bTables a left join eTableStatus b on a.TableCode=b.TableNo order by HallCode, a.TableCode";
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        $row = $model->query($query);
        $result = array();
        $arr = array('kongbai' => 0, 'wei' => 0, 'yi' => 0, 'jie' => 0);
        $price = array('kongbai' => 0, 'wei' => 0, 'yi' => 0, 'jie' => 0);
        foreach ((array)$row as $list) {
            if ($list['Total'] != null) {
                $list['Total'] = str_replace('.00', '', number_format($list['Total'], 2, '.', ''));
            }
            if ($list['IfClose'] == '√') {
                $list['number'] = 4;
                $price['jie'] = $price['jie'] + $list['Total'];
                $arr['jie'] = $arr['jie'] + 1;
            } else if ($list['Total'] > 0) {
                $list['number'] = 3;
                $price['yi'] = number_format($price['yi'] + $list['Total'], 2, '.', '');
                $arr['yi'] = $arr['yi'] + 1;
            } else if ($list['Total'] == 0 && $list['Total'] != null) {
                $list['number'] = 2;
                $arr['wei'] = $arr['wei'] + 1;
            } else if ($list['Total'] == null) {
                $list['number'] = 1;
                $arr['kongbai'] = $arr['kongbai'] + 1;
            }
            $result[$list['HallName']][] = $list;
        }
        exit(json_encode(array('code' => 200, 'num' => $arr, 'price' => $price, 'data' => $result)));
    }

    public function canzhuo_detail()
    {
        $name = Request::param('name') ? Request::param('name') : '';
        $query = "select AccountNo from eTableStatus where TableName='" . $name . "'";
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        $row = $model->query($query);

        $sql = "select TableName,Total, HallService,PersonNum,OUserName,OpenDateTime  from eTableStatus where AccountNo='" . $row[0]['AccountNo'] . "'";

        $account = $model->query($sql);
        foreach ((array)$account as $k => $v) {
            $account[$k]['OpenDateTime'] = date('H:i:s', strtotime($v['OpenDateTime']));
            $account[$k]['HallService'] = number_format($v['HallService'], 2, '.', '');
            $account[$k]['Total'] = number_format($v['Total'], 2, '.', '');
        }

        $sqls = "select DishName,Unit,Price,QtyNum,Total from eOrderDish_day where AccountNo='" . $row[0]['AccountNo'] . "' 
order by XH";
        $detail = $model->query($sqls);

        exit(json_encode(array('code' => 200, 'detail' => $detail, 'data' => $row, 'account' => $account[0])));
    }

    public function zhangdan()
    {
        $type = Request::param('type') ? Request::param('type') : 1;
        $date = Request::param('date') ? Request::param('date') : date('Y-m-d');
        $month = Request::param('month') ? Request::param('month') : date('Y-m');
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        if ($type == 1) {
            $sql = 'exec wxMoneyReport';
            $model->execute($sql);
        } else if ($type == 2) {
            $sql = "exec wxMoneyReport_day '" . $date . "'";
            $model->execute($sql);
        } else if ($type == 3) {
            $sql = "exec wxMoneyReport_Mon '" . $month . "'";
            $model->execute($sql);
        }
        $query = "select * from rTimeMan a inner join bTime b on a.TimeName=b.XhName order by  xh";
        $row = $model->query($query);
        $result = array();
        $day = array();
        $person = array();
        $prices = array();
        $price = 0;
        $number = 0;
        $pie_data1 = array();

        $arr1 = array();
        $pie1 = array();
        $price1 = 0;
        $pie_data1 = array();
        $max = 0;
        foreach ((array)$row as $list) {
            if ($list['TimeNum'] > $max) {
                $max = $list['TimeNum'];
            }
            $list['TimeTotal'] = str_replace('.00', '', number_format($list['TimeTotal'], 2, '.', ''));

            $result[] = $list;
            $price += $list['TimeTotal'];
            $number += $list['TimeNum'];

            $a = array();
            $a['name'] = $list['TimeName'] . '￥' . $list['TimeTotal'];
            $a['value'] = $list['TimeTotal'];
            $pie1[] = $a;
            $pie_data1[] = $list['TimeName'] . '￥' . $list['TimeTotal'];
            $price1 += $list['TimeTotal'];
        }
        exit(json_encode(array('code' => 200, 'data' => $result, 'pie1' => $pie1, 'price1' => $price1, 'pie_data1' => $pie_data1, 'price' => $price, 'number' => $number, 'person' => $person, 'max' => $max, 'prices' => $prices, 'day' => $day)));
    }

    public function caozuo()
    {
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        $query = "select DishName,sum(QtyNum) as QtyNum,sum(Total) as Total from eCancelPresent_day where Status='×' group by DishName order by QtyNum Desc";
        $row = $model->query($query);
        $tui_data = array();
        $tui_price = 0;
        $tui_num = 0;
        foreach ((array)$row as $tui_list) {
            $tui_list['QtyNum'] = intval($tui_list['QtyNum']);
            $tui_price += $tui_list['Total'];
            $tui_num += $tui_list['QtyNum'];
            $tui_data[] = $tui_list;
        }
        $query = "select DishName,sum(QtyNum) as QtyNum,sum(Total) as Total from eCancelPresent_day where Status='√' group by DishName order by QtyNum Desc";
        $row = $model->query($query);
        $zeng_data = array();
        $zeng_price = 0;
        $zeng_num = 0;
        foreach ((array)$row as $zeng_list) {
            $zeng_list['QtyNum'] = intval($zeng_list['QtyNum']);
            $zeng_price += $zeng_list['Total'];
            $zeng_num += $zeng_list['QtyNum'];
            $zeng_data[] = $zeng_list;
        }

        $query = "select DishName,sum(QtyNum) as QtyNum,sum(DiscPrice) as Total from eOrderDish_day where DiscPrice>0 group by DishName order by QtyNum Desc";
        $row = $model->query($query);
        $ze_data = array();
        $ze_price = 0;
        $ze_num = 0;
        foreach ((array)$row as $ze_list) {
            $ze_list['QtyNum'] = intval($ze_list['QtyNum']);
            $ze_price += $ze_list['Total'];
            $ze_num += $ze_list['QtyNum'];
            $ze_data[] = $ze_list;
        }

        $query = "select UserName,AccountNo,Memo from eOlog where OFunction='消单' and Convert(varchar(10),ODateTime,120)='" . date('Y-m-d') . "'";
        $row = $model->query($query);
        $xiao_data = array();
        $xiao_price = 0;
        $xiao_num = 0;
        foreach ((array)$row as $xiao_list) {
            $xiao_data[] = $xiao_list;
        }

        // $query ="select count(*) from eOlog where OFunction='消单' and Convert(varchar(10),ODateTime,120)='".date('Y-m-d')."'";
        // $row = $model->query($query);
        // foreach((array)$row as $x_list){
        //     $xiao_num = floatval($x_list['computed']);
        // }

        exit(json_encode(array('code' => 200, 'xiao_data' => $xiao_data, 'xiao_num' => $xiao_num, 'tui_num' => $tui_num, 'tui_price' => $tui_price, 'tui_data' => $tui_data, 'ze_num' => $ze_num, 'ze_price' => number_format($ze_price, 2, '.', ''), 'ze_data' => $ze_data, 'zeng_num' => $zeng_num, 'zeng_price' => $zeng_price, 'zeng_data' => $zeng_data)));
    }

    public function order()
    {
        $type = Request::param('type') ? Request::param('type') : 1;
        $date = Request::param('date') ? Request::param('date') : date('Y-m-d');
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        if ($type == 1) {
            $query = "select AccountNo,TableName,PayTotal from eAccount_day order by AccountNo";
            $row = $model->query($query);
        } else if ($type == 2) {
            $query = "select AccountNo,TableName,PayTotal from eAccount_his where BussDate='" . $date . "' order by AccountNo";
            $row = $model->query($query);
        }
        exit(json_encode(array('code' => 200, 'data' => $row)));
    }

    public function order_detail()
    {
        $return = [];
        $type = Request::param('type') ? Request::param('type') : 1;
        $date = Request::param('date') ? Request::param('date') : 1;
        $AccountNo = Request::param('AccountNo') ? Request::param('AccountNo') : '';
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        if ($type == 1) {
            //--今日帐单页，上部分数据的查询命令
            $query = "select TableName,Total,HallService,PersonNum,OUserName,OpenDateTime From eAccount_day where AccountNo='" . $AccountNo . "'";
            $tabList = $model->query($query);

            $query = "select DishName,Unit,Price,QtyNum,Total From eOrderDish_day where AccountNo='" . $AccountNo . "' order by xh";
            $return['list'] = $model->query($query);
        } else {
            //历史帐单页，上部分数据的查询命令
            $query = "select TableName,Total,HallService,PersonNum,OUserName,OpenDateTime From eAccount_his where BussDate='" . $date . "' and AccountNo='" . $AccountNo . "'";
            $tabList = $model->query($query);

            $query = "select DishName,Unit,Price,QtyNum,Total From eOrderDish_his where BussDate='" . $date . "' and AccountNo='" . $AccountNo . "' order by xh";
            $return['list'] = $model->query($query);
        }
        if (!empty($tabList)) {
            $arr = [];
            $arr[] = [
                'num' => $tabList[0]['TableName'],
                'name' => '餐台名称'
            ];
            $arr[] = [
                'num' => $tabList[0]['Total'],
                'name' => '消费金额'
            ];
            $arr[] = [
                'num' => $tabList[0]['HallService'] == '.00' ? '0.00' : $tabList[0]['HallService'],
                'name' => '服务费'
            ];
            $arr[] = [
                'num' => intval($tabList[0]['PersonNum']),
                'name' => '人数'
            ];
            $arr[] = [
                'num' => $tabList[0]['OUserName'],
                'name' => '开台人'
            ];
            $arr[] = [
                'num' => date('H:i:s', strtotime($tabList[0]['OpenDateTime'])),
                'name' => '开台时间'
            ];
            $return['tabList'] = $arr;
        }
        exit(json_encode(array('code' => 200, 'data' => $return)));
    }

    public function zhidai()
    {
        $type = Request::param('type') ? Request::param('type') : 1;
        $year = Request::param('year') ? Request::param('year') : date('Y');
        $month = Request::param('month') ? Request::param('month') : date('Y-m');
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        if ($type == 1) {
            $query = "select a.DishBName,DishName,c.Price  from bDishs a inner join bDishB b on a.DishBCode=b.DishBCode  inner join bDishUnitPrice c on a.DishCode=c.DishCode
     where a.DishCode not in (select DishCode from eOrderDish_his where convert(varchar(7),BussDate,120)='" . $month . "' group by DishCode) 
     order by a.DishCode";
            $row = $model->query($query);
        } else if ($type == 2) {
            $query = "select a.DishBName,DishName,c.Price  from bDishs a inner join bDishB b on a.DishBCode=b.DishBCode  inner join bDishUnitPrice c on a.DishCode=c.DishCode
     where a.DishCode not in (select DishCode from eOrderDish_his where convert(varchar(7),BussDate,120)>='2021-01' and convert(varchar(7),BussDate,120)<='2021-03' group by DishCode) 
     order by a.DishCode";
            $row = $model->query($query);
        } else if ($type == 3) {
            $query = "select a.DishBName,DishName,c.Price  from bDishs a inner join bDishB b on a.DishBCode=b.DishBCode  inner join bDishUnitPrice c on a.DishCode=c.DishCode
     where a.DishCode not in (select DishCode from eOrderDish_his where convert(varchar(4),BussDate,120)='" . $year . "' group by DishCode) 
     order by a.DishCode";
            $row = $model->query($query);
        }
        $result = array();
        $key = array();
        $arr = [];
        foreach ((array)$row as $list) {
            if (!in_array($list['DishBName'], $arr)) {
                $arr[] = $list['DishBName'];
            }
            $num = '';
            foreach ($arr as $k => $v) {
                if ($v == $list['DishBName']) {
                    $num = $k;
                }
            }
            $result[$num][] = $list;
        }
        exit(json_encode(array('code' => 200, 'data' => $result)));
    }

    public function shiduan()
    {
        $type = Request::param('type') ? Request::param('type') : 1;
        $date = Request::param('date') ? Request::param('date') : date('Y-m-d');
        $month = Request::param('month') ? Request::param('month') : date('Y-m');
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        if ($type == 1) {
            $sql = 'exec wxTimeReport';
            $model->execute($sql);

            $query = "select * from rTimeMan order by TimeName";
            $row = $model->query($query);
        } else if ($type == 2) {
            $sql = "exec wxTimeReport_day '" . $date . "'";
            $model->execute($sql);

            $query = "select * from rTimeMan order by TimeName";
            $row = $model->query($query);
        } else if ($type == 3) {
            $sql = "exec wxTimeReport_Mon '" . $month . "'";
            $model->execute($sql);

            $query = "select DepartName,sum(CTotal) as Total from eOrderDish_his where convert(varchar(7),BussDate,120)='" . $month . "' group by DepartName
    ";
            $row = $model->query($query);
        }
        $result = array();
        $day = array();
        $person = array();
        $prices = array();
        $price = 0;
        $number = 0;
        $pie_data1 = array();
        $max = 0;
        foreach ((array)$row as $list) {
            if ($list['TimeNum'] > $max) {
                $max = $list['TimeNum'];
            }

            $list['TimeTotal'] = str_replace('.00', '', number_format($list['TimeTotal'], 2, '.', ''));
            $list['child'] = $child;

            $result[] = $list;
            $price += $list['TimeTotal'];
            $number += $list['TimeNum'];

            $day[] = $list['TimeRouand'];
            $prices[] = $list['TimeTotal'];
            $person[] = $list['TimeNum'];
        }
        exit(json_encode(array('code' => 200, 'data' => $result, 'price' => $price, 'number' => $number, 'person' => $person, 'max' => $max, 'prices' => $prices, 'day' => $day, 'row' => $row)));
    }

    public function bumen()
    {
        $type = Request::param('type') ? Request::param('type') : 1;
        $date = Request::param('date') ? Request::param('date') : date('Y-m-d');
        $month = Request::param('month') ? Request::param('month') : date('Y-m');
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        if ($type == 1 || ($type == 2 && strtotime($date) == strtotime(date('Y-m-d')))) {
            $query = "select DepartName,sum(CTotal) as Total from eOrderDish_day group by DepartName";
            $row = $model->query($query);
        } else if ($type == 2) {
            $query = "select DepartName,sum(CTotal) as Total from eOrderDish_his where BussDate='" . $date . "' group by DepartName";
            $row = $model->query($query);
        } else if ($type == 3) {
            $query = "select DepartName,sum(CTotal) as Total from eOrderDish_his where convert(varchar(7),BussDate,120)='" . $month . "' group by DepartName";
            $row = $model->query($query);
        }
        $result = array();
        $arr1 = array();
        $pie1 = array();
        $price = 0;
        $pie_data1 = array();
        foreach ((array)$row as $list) {
            $child = array();
            if ($type == 1) {
                $query1 = "select DishName,sum(QtyNum) as Num,sum(CTotal) as Total from eOrderDish_day where DepartName='" . $list['DepartName'] . "' group by DishName order by Num Desc";
                $row1 = $model->query($query1);
            } else if ($type == 2) {
                $query1 = "select DishName,sum(QtyNum) as Num,sum(CTotal) as Total from eOrderDish_his where BussDate='" . $date . "' and DepartName='" . $list['DepartName'] . "' group by DishName order by Num Desc";
                $row1 = $model->query($query1);
            } else if ($type == 3) {
                $query1 = "select cast(DishName as nvarchar(100)) as DishName,sum(QtyNum) as Num,sum(CTotal) as Total from eOrderDish_his where convert(varchar(7),BussDate,120)='" . $month . "' and DepartName='" . $list['DepartName'] . "' group by DishName order by Num Desc";
                $row1 = $model->query($query1);
            }
            $count_num = 0;
            $count_price = 0;
            if ($type < 3) {
                foreach ((array)$row1 as $list1) {
                    $list1['Total'] = str_replace('.00', '', number_format($list1['Total'], 2, '.', ''));
                    $count_price += $list1['Total'];
                    $count_num += $list1['Num'];
                    $child[] = $list1;
                }
            }
            $list['count_price'] = number_format($count_price, 2, '.', '');
            $list['count_num'] = number_format($count_num, 2, '.', '');
            $list['Total'] = str_replace('.00', '', number_format($list['Total'], 2, '.', ''));
            $list['child'] = $child;

            $result[] = $list;

            $a = array();
            $a['name'] = $list['DepartName'] . '￥' . $list['Total'];
            $a['value'] = $list['Total'];
            $pie1[] = $a;
            $pie_data1[] = $list['DepartName'] . '￥' . $list['Total'];
            $price += $list['Total'];
        }
        exit(json_encode(array('code' => 200, 'data' => $result, 'pie1' => $pie1, 'price' => $price, 'pie_data1' => $pie_data1)));
    }

    public function leibie()
    {
        $type = Request::param('type') ? Request::param('type') : 1;
        $date = Request::param('date') ? Request::param('date') : date('Y-m-d');
        $month = Request::param('month') ? Request::param('month') : date('Y-m');
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        if ($type == 1 || ($type == 2 && strtotime(date('Y-m-d')) == strtotime($date))) {
            $query = "select DishBName,sum(CTotal) as Total from eOrderDish_day group by DishBName order by Total Desc";
            $row = $model->query($query);
        } else if ($type == 2) {
            $query = "select DishBName,sum(CTotal) as Total from eOrderDish_his where BussDate='" . $date . "' group by DishBName";
            $row = $model->query($query);
        } else if ($type == 3) {
            $query = "select DishBName,sum(CTotal) as Total from eOrderDish_his 
    where convert(varchar(7),BussDate,120)='" . $month . "' group by DishBName order by Total Desc";
            $row = $model->query($query);
        }
        $result = array();
        $arr1 = array();
        $pie1 = array();
        $price = 0;
        $pie_data1 = array();
        foreach ((array)$row as $list) {
            if ($type == 1) {
                $query1 = "select DishName,sum(QtyNum) as Num,sum(CTotal) as Total from eOrderDish_day where DishBName='" . $list['DishBName'] . "' group by DishName order by Num Desc";
                $row1 = $model->query($query1);
                $child = array();
            } else if ($type == 2) {
                $query1 = "select DishName,sum(QtyNum) as Num,sum(CTotal) as Total from eOrderDish_his
     where BussDate='" . $date . "' and DishBName='" . $list['DishBName'] . "' group by DishName order by Num Desc";
                $row1 = $model->query($query1);
                $child = array();
            } else if ($type == 3) {
                $query1 = "select DishName,sum(QtyNum) as Num,sum(CTotal) as Total from eOrderDish_his
     where convert(varchar(7),BussDate,120)='" . $month . "' and DishBName='" . $list['DishBName'] . "'
     group by DishName order by Num Desc";
                $row1 = $model->query($query1);
                $child = array();
            }
            $count_num = 0;
            $count_price = 0;
            foreach ((array)$row1 as $list1) {
                $count_price += $list1['Total'];
                $count_num += $list1['Num'];
                $list1['AvgTotal'] = str_replace('.00', '', number_format($list1['Total'], 2, '.', ''));
                $child[] = $list1;
            }
            $list['count_price'] = number_format($count_price, 2, '.', '');
            $list['count_num'] = $count_num;
            $list['Total'] = str_replace('.00', '', number_format($list['Total'], 2, '.', ''));
            $list['child'] = $child;

            $result[] = $list;

            $a = array();
            $a['name'] = $list['DishBName'] . '￥' . $list['Total'];
            $a['value'] = $list['Total'];
            $pie1[] = $a;
            $pie_data1[] = $list['DishBName'] . '￥' . $list['Total'];
            $price += $list['Total'];
        }
        exit(json_encode(array('code' => 200, 'data' => $result, 'pie1' => $pie1, 'price' => $price, 'pie_data1' => $pie_data1)));
    }

    public function fantai()
    {
        $date = Request::param('date') ? Request::param('date') : date('Y-m-d');
        $month = Request::param('month') ? Request::param('month') : date('Y-m');
        $type = Request::param('type') ? Request::param('type') : 1;
        if ($type == 3) {
            $date1 = date('Y-m-01', strtotime($month));
            $date2 = date('Y-m-d', strtotime(date('Y-m-01', strtotime('+1 month'))) - 1);
        } else {
            $date1 = $date2 = $date;
        }
        $num = $date == date('Y-m-d') ? 1 : 0;
        $sql = "exec ProB_rptHallTableUsing '" . $date1 . "','" . $date2 . "'," . $num . ",'00:00:01','23:59:59'";
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        $model->execute($sql);
        $query = "select * From rTableUsing";
        $row = $model->query($query);
        $result = [];
        $arr = [];
        $count_price = 0;
        foreach ((array)$row as $list) {
            if ($list['PayTotal'] != null) {
                $list['PayTotal'] = str_replace('.00', '', number_format($list['PayTotal'], 2, '.', ''));
            }
            if ($list['AvgTotal'] != null) {
                $list['AvgTotal'] = str_replace('.00', '', number_format($list['AvgTotal'], 2, '.', ''));
            }
            if (!in_array($list['HallName'], $arr)) {
                $arr[] = $list['HallName'];
            }
            $num = '';
            foreach ($arr as $k => $v) {
                if ($v == $list['HallName']) {
                    $num = $k;
                }
            }
            $result[$num][] = $list;
            $count_price += $list['PayTotal'];
        }
        $pie1 = array();
        $pie_data1 = array();
        foreach ($result as $k => $v) {
            $nums = 0;
            $price = 0;
            foreach ($v as $kk => $vv) {
                $nums += $vv['Num'];
                $price += $vv['PayTotal'];
                $result[$k][$kk]['NumC'] = floatval($vv['NumC']);
            }
            $result[$k][0]['number'] = $nums;
            $result[$k][0]['price'] = $price;

            $HallName = $result[$k][0]['HallName'];
            $a = array();
            $a['name'] = $HallName . '¥' . $price;
            $a['value'] = $price;
            $pie1[] = $a;
            $pie_data1[] = $HallName . '¥' . $price;
        }
        //echo '<pre>';print_r($pie1);die;
        exit(json_encode(array('code' => 200, 'data' => $result, 'price' => $count_price, 'pie1' => $pie1, 'pie_data1' => $pie_data1)));
    }

    public function ribao()
    {
        $date = Request::param('date') ? Request::param('date') : date('Y-m-d');
        $sql = "exec wxRecReport_Day '" . $date . "'";

        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        $model->execute($sql);
        $query = "select Project,Content from rBussDayReport order by xh";
        $row = $model->query($query);
        $result = array();
        $arr = array();
        $pie = array();
        $pie_data = array();
        $isPie = false;
        $price = 0;
        foreach ((array)$row as $list) {
            if ($list['Project'] == '总营业额') {
                $list['Content'] = str_replace('.00', '', number_format($list['Content'], 2, '.', ''));
                $result[] = $list;
            } else if ($list['Project'] == '目前营收' || $list['Project'] == '应收金额') {
                $list['Content'] = str_replace('.00', '', number_format($list['Content'], 2, '.', ''));
                $result[] = $list;
            } else if ($list['Project'] == '帐单数' || $list['Project'] == '总 单 数') {
                $result[] = $list;
            } else if ($list['Project'] == '客流量' || $list['Project'] == '总 人 数') {
                $result[] = $list;
            } else if ($list['Project'] == '人均消费') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            } else if ($list['Project'] == '台均消费') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            } else if ($list['Project'] == '退菜金额' || $list['Project'] == '退菜') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            } else if ($list['Project'] == '赠送金额' || $list['Project'] == '赠送') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            } else if ($list['Project'] == '折扣总额') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            }
            if ($isPie) {
                $list['Content'] = str_replace('.00', '', number_format($list['Content'], 2, '.', ''));
                $a = array();
                $a['name'] = $list['Project'] . '￥' . $list['Content'];
                $a['value'] = $list['Content'];
                $pie[] = $a;
                $pie_data[] = $list['Project'] . '￥' . $list['Content'];
                $price += $list['Content'];
            }
            if ($list['Project'] == '付款汇总') {
                $isPie = true;
            }
            $arr[] = $list;
        }

        $date = Request::param('date') ? Request::param('date') : date('Y-m-d');
        $query1 = "select DepartName,sum(CTotal) as Total from eOrderDish_his where BussDate='" . $date . "' group by DepartName";
        $row1 = $model->query($query1);
        $result1 = array();
        $arr1 = array();
        $pie1 = array();
        $price1 = 0;
        $pie_data1 = array();
        foreach ((array)$row1 as $list1) {
            $list1['Total'] = number_format($list1['Total'], 2, '.', '');
            $result1[] = $list;

            $a = array();
            $a['name'] = $list1['DepartName'] . '￥' . $list1['Total'];
            $a['value'] = $list1['Total'];
            $pie1[] = $a;
            $pie_data1[] = $list1['DepartName'] . '￥' . $list1['Total'];
            $price1 += $list1['Total'];
        }

        $date1 = Request::param('date') ? date('Y-m', strtotime(Request::param('date'))) : date('Y-m');
        $query2 = "select BussDate,isnull(sum(PersonNum),0) as PersonNum,isnull(sum(PayTotal),0) as Total from eAccount_his where convert(varchar(7),BussDate,120)='" . $date1 . "' group by BussDate
    ";
        $row2 = $model->query($query2);
        $result2 = array();
        $day = array();
        $price2 = array();
        $person = array();
        $max = 0;
        foreach ((array)$row2 as $list2) {
            if ($list2['PersonNum'] > $max) {
                $max = $list2['PersonNum'];
            }
            $list2['BussDate'] = iconv('gb2312', 'utf-8', $list2['BussDate']);
            $BussDate = explode(' ', $list2['BussDate']);

            $list2['day'] = intval(date('m', strtotime($_REQUEST['date']))) . '月' . $BussDate[2] . '日';
            $result2[] = $list2;
            $day[] = $list2['day'];
            $price2[] = $list2['Total'];
            $person[] = $list2['PersonNum'];
        }
        exit(json_encode(array('code' => 200, 'pie' => $pie, 'price' => $price, 'pie_data' => $pie_data, 'data' => $result, 'pie1' => $pie1, 'price1' => $price1, 'pie_data1' => $pie_data1, 'price2' => $price2, 'day' => $day, 'person' => $person, 'data2' => $result2)));
    }

    public function yuebao()
    {
        $date = Request::param('date') ? Request::param('date') : date('Y-m');
        $sql = "exec wxRecReport_His '" . $date . "'";
        $model = Db::connect('sqlsrv://' . $this->data_user . ':' . $this->data_pwd . '@' . $this->data_ip . ':' . $this->data_num . '/' . $this->data_name . '#utf8');
        $model->execute($sql);
        $query = "select Project,Content from rBussDayReport order by xh";
        $row = $model->query($query);
        $result = array();
        $arr = array();
        $pie = array();
        $pie_data = array();
        $isPie = false;
        $price = 0;
        foreach ((array)$row as $list) {
            if ($list['Project'] == '总营业额') {
                $list['Content'] = str_replace('.00', '', number_format($list['Content'], 2, '.', ''));
                $result[] = $list;
            } else if ($list['Project'] == '目前营收' || $list['Project'] == '应收金额') {
                $list['Content'] = str_replace('.00', '', number_format($list['Content'], 2, '.', ''));
                $result[] = $list;
            } else if ($list['Project'] == '帐单数' || $list['Project'] == '总 单 数') {
                $result[] = $list;
            } else if ($list['Project'] == '客流量' || $list['Project'] == '总 人 数') {
                $result[] = $list;
            } else if ($list['Project'] == '人均消费') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            } else if ($list['Project'] == '台均消费') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            } else if ($list['Project'] == '退菜金额' || $list['Project'] == '退菜') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            } else if ($list['Project'] == '赠送金额' || $list['Project'] == '赠送') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            } else if ($list['Project'] == '折扣总额') {
                $list['Content'] = number_format($list['Content'], 2, '.', '');
                $result[] = $list;
            }
            if ($isPie) {
                $list['Content'] = str_replace('.00', '', number_format($list['Content'], 2, '.', ''));
                $a = array();
                $a['name'] = $list['Project'] . '￥' . $list['Content'];
                $a['value'] = $list['Content'];
                $pie[] = $a;
                $pie_data[] = $list['Project'] . '￥' . $list['Content'];
                $price += $list['Content'];
            }
            if ($list['Project'] == '付款汇总') {
                $isPie = true;
            }
            $arr[] = $list;
        }

        $query1 = "select DepartName,sum(CTotal) as Total from eOrderDish_his where convert(varchar(7),BussDate,120)='" . $date . "' group by DepartName";

        $row1 = $model->query($query1);
        $result1 = array();
        $arr1 = array();
        $pie1 = array();
        $price1 = 0;
        $pie_data1 = array();
        foreach ((array)$row1 as $list1) {
            $list1['Total'] = number_format($list1['Total'], 2, '.', '');
            $result1[] = $list;

            $a = array();
            $a['name'] = $list1['DepartName'] . '￥' . $list1['Total'];
            $a['value'] = $list1['Total'];
            $pie1[] = $a;
            $pie_data1[] = $list1['DepartName'] . '￥' . $list1['Total'];
            $price1 += $list1['Total'];
        }

        $date1 = Request::param('date') ? date('Y', strtotime(Request::param('date'))) : date('Y');
        $query2 = "select convert(varchar(7),BussDate,120) as Month,isnull(sum(PersonNum),0) as PersonNum,isnull(sum(PayTotal),0) as Total from eAccount_his 
    where convert(varchar(4),BussDate,120)='" . $date1 . "'  group by convert(varchar(7),BussDate,120)";
        $row2 = $model->query($query2);
        $result2 = array();
        $day = array();
        $price2 = array();
        $person = array();
        $max = 0;
        foreach ((array)$row2 as $list2) {
            if ($list2['PersonNum'] > $max) {
                $max = $list2['PersonNum'];
            }

            $list2['day'] = $list2['Month'];
            $result2[] = $list2;
            $day[] = $list2['day'];
            $price2[] = $list2['Total'];
            $person[] = $list2['PersonNum'];
        }

        exit(json_encode(array('code' => 200, 'pie' => $pie, 'price' => $price, 'pie_data' => $pie_data, 'data' => $result, 'pie1' => $pie1, 'price1' => $price1, 'pie_data1' => $pie_data1, 'price2' => $price2, 'day' => $day, 'person' => $person, 'data2' => $result2, 'date' => $date)));
    }
}
